package l.com.tableview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;

public class TableView2 extends ViewGroup {
    boolean canReLayout = true;
    private int touchSlop;
    private TableAdapter adapter;
    int width, height;
    int rowCount;
    int columnCount;
    Recycler recycler;
    int downX = 0;
    int downY = 0;

    public TableView2(Context context) {
        this(context, null);
    }

    public TableView2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TableView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();


        // TODO: 2019/1/16  思路是什么?
        //我要写一个view
        //onMeasure 测量宽高
        //宽是什么,高是什么? 需要什么东西?
        //需要adapter提供每一个item对应的宽高
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int w = 0;
        int h = 0;
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (adapter != null) {
            rowCount = adapter.getRowCount();
            columnCount = adapter.getColumnCount();
            //记录宽度和高度的最大值
            //需要知道每一行的宽度,和每一列的高度
            //每一行的宽度是每个item的宽度的累加,高度不变
            int[] widths = new int[columnCount];
            int[] heights = new int[rowCount];
            for (int i = 0; i < widths.length; i++) {
                widths[i] = adapter.getWidth(i);
            }
            for (int i = 0; i < heights.length; i++) {
                heights[i] = adapter.getHeight(i);
            }
            if (widthMode == MeasureSpec.AT_MOST) {
                w = Math.min(widthSize, sumArrays(widths));
            } else {
                w = widthSize;
            }

            if (heightMode == MeasureSpec.AT_MOST) {
                h = Math.min(heightSize, sumArrays(heights));
            } else {
                h = heightSize;
            }
        }
        setMeasuredDimension(w, h);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (changed || canReLayout) {
            canReLayout = false;
            if (adapter == null) {
                return;
            }
            width = r - l;
            height = b - t;
            int left = 0;
            int top = 0;
            int right;
            int bottom;
            

            for (int i = 0; i < rowCount && top < height; i++) {
                bottom = top + adapter.getHeight(i);
                left = 0;
                for (int j = 0; j < columnCount && left < width; j++) {
                    right = left + adapter.getWidth(j);
                    makeAndStepView(i, j, left, top, right, bottom);
                    left = right;
                }
                top = bottom;

            }
        }

        //测量完成,如何摆放呢?
        //  View view = adapter.getview
        //   view.layout(left,top,right,bottom);

    }

    private void makeAndStepView(int row, int column, int left, int top, int right, int bottom) {
        int itemType = adapter.getItemType(row, column);
        //复用的view应该从recycler中获取
        View recycledView = recycler.getRecycledView(itemType);

        View convertView = adapter.getView(row, column, recycledView, this);
        if (convertView == null) {
            throw new RuntimeException("convertView不能为空,需要实例化");
        }
        int height = bottom - top;
        int width = right - left;

        convertView.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
        convertView.layout(left, top, right, bottom);
        addView(convertView);
    }

    private int sumArrays(int[] arrays) {
        int sum = 0;
        for (int i = 0; i < arrays.length; i++) {
            sum += arrays[i];
        }
        return sum;
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean intercept = false;

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) ev.getX();
                downY = (int) ev.getY();

                break;
            case MotionEvent.ACTION_MOVE:
                int dx = (int) Math.abs(ev.getX() - downX);
                int dy = (int) Math.abs(ev.getY() - downY);
                if (dx > touchSlop || dy > touchSlop) {
                    intercept = true;
                }

                break;
            case MotionEvent.ACTION_UP:
                break;

        }

        return intercept;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) ev.getX();
                downY = (int) ev.getY();

                break;
            case MotionEvent.ACTION_MOVE:
                int dx = (int) (ev.getX() - downX);
                int dy = (int) (ev.getY() - downY);
                scrollBy(-dx, -dy);
                downX = (int) ev.getX();
                downY = (int) ev.getY();

                //如何确定滑动的左边界和上边界


                break;
            case MotionEvent.ACTION_UP:
                break;

        }
        return true;
    }

    public void setAdapter(TableAdapter adapter) {
        this.adapter = adapter;
        recycler = new Recycler(adapter.getTypeCount());
        canReLayout = true;
        requestLayout();
    }
}
